package com.rc.saas.tenant.controller.market;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.DateUtil;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.component.UploadComponent;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.elec.ElecStationExample;
import com.rc.saas.tenant.model.market.*;
import com.rc.saas.tenant.model.tenant.Tenant;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.model.ticket.TenantTicketType;
import com.rc.saas.tenant.model.ticket.TenantTicketTypeExample;
import com.rc.saas.tenant.search.activity.ActivityMemberSearch;
import com.rc.saas.tenant.search.market.MarketSearch;
import com.rc.saas.tenant.search.order.OrderRefundApplySearch;
import com.rc.saas.tenant.search.ticket.TicketSearch;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.service.market.MarketActivityRecordService;
import com.rc.saas.tenant.service.market.MarketActivityService;
import com.rc.saas.tenant.service.tenant.TenantService;
import com.rc.saas.tenant.service.ticket.TenantTicketTypeService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.market.MarketActivityDiscountVo;
import com.rc.saas.tenant.vo.market.MarketActivityRecordVo;
import com.rc.saas.tenant.vo.market.MarketActivityVo;
import com.rc.saas.tenant.vo.order.OrderRefundApplyVo;
import com.rc.saas.tenant.vo.ticket.TicketVo;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2021/9/16
 */
@Controller
@RequestMapping("/marketActivity")
public class MarketActivityController {

    private static Logger logger = LogManager.getLogger(MarketActivityController.class);

    @Autowired
    private MarketActivityService marketActivityService;

    @Autowired
    private TenantTicketTypeService tenantTicketTypeService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private ElecStationService elecStationService;

    @Autowired
    private UploadComponent uploadComponent;

    @Autowired
    private MarketActivityRecordService marketActivityRecordService;


    /*    *//**
     * 跳转营销活动页面
     *
     * @return
     *//*
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        TenantUser user = ShiroUtils.getSessionUser();
        Tenant tenant = tenantService.selectByPrimaryKey(user.getTenantCode());
        model.addAttribute("orginId", tenant.getWechatOriginalid());
        if (isExecute(user)) {
            return "/templates/market/marketActivity";
        } else {
            return "/templates/market/marketActivityRecordNoReturn";
        }

    }*/

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return "/templates/market/marketActivity";
    }


    @RequestMapping(value = "/listPageMarketActivity", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageMarketActivity(MarketSearch marketSearch) {
        PageInfo<MarketActivity> pageInfo = marketActivityService.listPageMarketActivity(marketSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    @RequestMapping(value = "/listPageActivityMemberInfo", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageActivityMemberInfo(ActivityMemberSearch activityMemberSearch) {
        if (!StringUtil.isNotBlank(activityMemberSearch.getMemberAddress())){
            activityMemberSearch.setMemberAddress("%"+activityMemberSearch.getMemberAddress()+"%");
        }
        PageInfo<MarketActivityMemberInfo> pageInfo = marketActivityService.listPageActivityMemberInfo(activityMemberSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }



    /*
     *//**
     * 返回桩群列表数据
     *
     * @return
     *//*
    @RequestMapping(value = "/listStation", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listStation(ElecSationSearch elecSationSearchVo) {
        //默认其他省为10000
        if ("10000".equals(elecSationSearchVo.getAreaId())) {
            TenantUser user = ShiroUtils.getSessionUser();
            String provinceId = elecStationService.selectMostProvinceId(user.getTenantCode());
            elecSationSearchVo.setAreaId(null);
            elecSationSearchVo.setProvinceId(provinceId);
        }
        PageInfo<ElecStationResult> pageInfo = elecStationService.listPageByCondition(elecSationSearchVo);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }*/


    /**
     * 返回营销活动列表数据
     *
     * @return
     */
    @RequestMapping(value = "/selectMarketActivity", method = RequestMethod.GET)
    @ResponseBody
    public TResponse selectMarketActivity(BaseSearch baseSearch) {
        MarketActivityVo marketActivity = marketActivityService.selectMarketActivityByTenantCode(baseSearch.getTenantCode());
        return new TResponse(0, "", new ArrayList() {{
            if (marketActivity != null) {
                add(marketActivity);
            }
        }});
    }

    /**
     * 跳转营销活动记录页面
     *
     * @return
     */
    @RequestMapping(value = "/marketActivityRecord", method = RequestMethod.GET)
    public String marketActivityRecord() {

        return "/templates/market/marketActivityRecord";
    }

    /**
     * 返回营销活动记录列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageMarketActivityRecord", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageMarketActivityRecord(BaseSearch baseSearch, String beginTime, String endTime) {
        PageInfo<MarketActivityRecordVo> pageInfo = marketActivityService.listPageMarketActivityRecord(baseSearch, beginTime, endTime);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转充值送营销活动页面
     *
     * @return
     */
    @RequestMapping(value = "/rechargeDiscountIndex", method = RequestMethod.GET)
    public String rechargeDiscountIndex() {
        return "/templates/market/rechargeDiscount";
    }


    /**
     * 返回充值送营销活动列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageRechargeDiscount", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageRechargeDiscount(BaseSearch baseSearch, String startTime, String endTime) throws ParseException {
        PageInfo<MarketActivity> pageInfo = marketActivityService.listPageRechargeDiscount(baseSearch, startTime, endTime);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转详情充值送营销活动
     *
     * @return
     */
    @RequestMapping(value = "/detailRechargeDiscount/{marketActivityId}", method = RequestMethod.GET)
    public String detailRechargeDiscount(Model model, @PathVariable("marketActivityId") Long marketActivityId) {
        MarketActivityDiscountVo marketActivityVo = getMarketActivityDiscountVo(marketActivityId);
        model.addAttribute("marketActivityVo", marketActivityVo);
        return "/templates/market/detailRechargeDiscount";
    }

    /**
     * 跳转编辑充值送营销活动
     *
     * @return
     */
    @RequestMapping(value = "/editRechargeDiscount/{marketActivityId}", method = RequestMethod.GET)
    public String editRechargeDiscount(Model model, @PathVariable("marketActivityId") Long marketActivityId) {

        MarketActivityDiscountVo marketActivityVo = getMarketActivityDiscountVo(marketActivityId);

        model.addAttribute("marketActivityVo", marketActivityVo);
        return "/templates/market/editRechargeDiscount";
    }

    private MarketActivityDiscountVo getMarketActivityDiscountVo(@PathVariable("marketActivityId") Long marketActivityId) {
        TenantUser user = ShiroUtils.getSessionUser();

        TenantTicketTypeExample example = new TenantTicketTypeExample();
        example.createCriteria().andStatusEqualTo("0").andTenantCodeEqualTo(user.getTenantCode()).andIsMaxEqualTo("1");
        List<TenantTicketType> tenantTicketTypes = tenantTicketTypeService.selectByExample(example);

        MarketActivityDiscountVo marketActivityVo = marketActivityService.selectDiscountAndTicket(marketActivityId);

        if ("2".equals(marketActivityVo.getRewardType())) {
            /**
             * 所有设置的优惠券
             */
            List<MarketActivityTicket> list = marketActivityVo.getList();

            for (MarketActivityTicket marketActivityTicket : list) {

                StringBuilder sb = new StringBuilder();
                for (TenantTicketType tenantTicketType : tenantTicketTypes) {
                    sb.append("<option value=\"");
                    sb.append(tenantTicketType.getTicketTypeId());

                    if (marketActivityTicket.getTicketTypeId().equals(tenantTicketType.getTicketTypeId())) {
                        sb.append("\" selected >");
                    } else {
                        sb.append("\">");
                    }
                    sb.append(tenantTicketType.getTicketName());
                    sb.append("</option>");
                }
                marketActivityTicket.setTicketSelect(sb.toString());
            }
        }
        return marketActivityVo;
    }

    /**
     * 跳转增加充值送营销活动
     *
     * @return
     */
    @RequestMapping(value = "/addRechargeDiscount", method = RequestMethod.GET)
    public String addRechargeDiscount() {
        return "/templates/market/addRechargeDiscount";
    }

    /**
     * 跳转增加营销活动页面
     *
     * @return
     */
    @RequestMapping(value = "/addMarketActivity", method = RequestMethod.GET)
    public String addMarketActivity(Model model) {
        TenantUser user = ShiroUtils.getSessionUser();

       /* TenantTicketTypeExample example = new TenantTicketTypeExample();
        example.createCriteria().andTenantCodeEqualTo(user.getTenantCode()).andStatusEqualTo("0")*//*.andIsMaxEqualTo("1")*//*;*/
        ElecStationExample elecStationExample = new ElecStationExample();
        List<ElecStation> stations = elecStationService.selectByExample(elecStationExample);
        Model stationInfos = model.addAttribute("stationInfos", stations);
        TicketSearch search = new TicketSearch();
        search.setStatus("0");
        List<TicketVo> ticketVo = tenantTicketTypeService.getTenantTickets(search);
        model.addAttribute("ticketInfos", ticketVo);

        return "/templates/market/addMarketActivity";
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public String findById( @PathVariable("id") Long id,Model model){
        ElecStationExample elecStationExample = new ElecStationExample();
        List<ElecStation> stations = elecStationService.selectByExample(elecStationExample);
        model.addAttribute("stationInfos", stations);
        TicketSearch search = new TicketSearch();
        search.setStatus("0");
        List<TicketVo> ticketVo = tenantTicketTypeService.getTenantTickets(search);
        model.addAttribute("ticketInfos", ticketVo);
        MarketActivity marketActivity = marketActivityService.findById(id);
        model.addAttribute("marketActivity",marketActivity);

        MarketActivityRecordExample childExample = new MarketActivityRecordExample();
        childExample.createCriteria().andMarketActivityIdEqualTo(marketActivity.getMarketActivityId());
        List<MarketActivityRecord> marketActivityRecords = marketActivityRecordService.selectByExample(childExample);
        model.addAttribute("marketActivityRecords",marketActivityRecords);
        model.addAttribute("sizes",marketActivityRecords.size());
        return "/templates/market/detailMarketAcivity";
    }


    /**
     * 跳转增加营销活动页面
     *
     * @return
     */
    @RequestMapping(value = "/editMarketActivity/{marketActivityId}", method = RequestMethod.GET)
    public String editMarketActivity(@PathVariable("marketActivityId") Long marketActivityId, Model model) {
        MarketActivity marketActivity = marketActivityService.selectByPrimaryKey(marketActivityId);

        TenantUser user = ShiroUtils.getSessionUser();

        TenantTicketTypeExample example = new TenantTicketTypeExample();
        example.createCriteria().andTenantCodeEqualTo(user.getTenantCode()).andStatusEqualTo("0").andIsMaxEqualTo("1");
        List<TenantTicketType> tenantTicketTypes = tenantTicketTypeService.selectByExample(example);

        model.addAttribute("tenantTicketTypes", tenantTicketTypes);
        model.addAttribute("marketActivity", marketActivity);
        return "/templates/market/editMarketActivity";
    }


    /**
     * 查询充值送优惠券
     *
     * @return
     */
    @RequestMapping(value = "/selectTicketType", method = RequestMethod.POST)
    @ResponseBody
    public TResponse selectTicketType() {
        TenantUser user = ShiroUtils.getSessionUser();
        try {
            TenantTicketTypeExample example = new TenantTicketTypeExample();
            example.createCriteria().andStatusEqualTo("0").andTenantCodeEqualTo(user.getTenantCode()).andIsMaxEqualTo("1");
            List<TenantTicketType> tenantTicketTypes = tenantTicketTypeService.selectByExample(example);
            return TResponse.SUCCESS_DATA(tenantTicketTypes);
        } catch (Exception e) {
            logger.error("查询充值送优惠券失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

    /**
     * 保存/编辑 充值送活动
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateRechargeDiscount", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateRechargeDiscount(MarketActivity marketActivity,@RequestParam(value = "file1", required = false) MultipartFile file1) {
        TenantUser user = ShiroUtils.getSessionUser();
        try {

            if (marketActivityService.isExecuteDiscount(marketActivity.getMarketActivityId(), user.getTenantCode(),
                    marketActivity.getAliPay(), marketActivity.getWxPay(), marketActivity.getUnPay(), marketActivity.getAbcPay(),marketActivity.getIsApp(),marketActivity.getIsWx()))
                return TResponse.FAIL("已存在对应支付方式的活动，请勿重复添加");


            List<MarketActivityTicket> marketActivityTicketList = JSONObject.parseArray(marketActivity.getTicketList(), MarketActivityTicket.class);

            Date date = new Date();
            marketActivity.setActivityType("1");
            marketActivity.setLastUpdatedTime(date);
            marketActivity.setLastUpdatedBy(user.getLoginName());
            if (marketActivity.getMarketActivityId() != null) {
                marketActivityService.updateDiscount(marketActivity, marketActivityTicketList);
            } else {
                marketActivity.setStatus("1");
                marketActivityService.insertDiscount(marketActivity, marketActivityTicketList);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存充值送活动失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 保存/编辑营销活动
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveActivity", method = RequestMethod.POST)
    public TResponse saveActivity(MarketActivity marketActivity,@RequestParam(value = "file1", required = false) MultipartFile file1) {

        TenantUser user = ShiroUtils.getSessionUser();
        try {
            if (file1 != null && file1.getSize() != 0 && file1.getContentType().contains("image")) {
                String imgUrl = uploadComponent.uploadFile(user.getTenantCode(), file1);
                marketActivity.setActivityImg(imgUrl);
            }
            marketActivity.setCreatedBy(user.getLoginName());
            marketActivity.setCreatedTime(new Date());
            if (StringUtils.isBlank(marketActivity.getParticipateUser())){
                marketActivity.setParticipateUser(null);
            }else if (marketActivity.getParticipateUser().startsWith(",")){
                String userStr = marketActivity.getParticipateUser().substring(1,marketActivity.getParticipateUser().length());
                if (StringUtils.isBlank(userStr)){
                    marketActivity.setParticipateUser(null);
                }else{
                    marketActivity.setParticipateUser(userStr);
                }
            }
            if (marketActivity.getContent().startsWith(",")){
                String contentStr = marketActivity.getContent().substring(1,marketActivity.getContent().length());
                if (StringUtils.isBlank(contentStr)){
                    marketActivity.setContent(null);
                }else{
                    if (contentStr.indexOf("img")>0){
                        contentStr = contentStr.replace("max-width:95%","max-width:100%");
                    }
                    marketActivity.setContent(contentStr);
                }
            }
            int code = marketActivityService.saveMarketActivity(marketActivity);
            if (code>0){
                logger.error("新增活动成功");
                return TResponse.SUCCESS("新增活动成功");
            }
          /*  if (marketActivity.getMarketActivityId() != null) {
                marketActivityService.updateMarketActivity(marketActivity);
            } else {
                if (isExecute(user)) return TResponse.FAIL("已经存在此种营销活动");

                marketActivityService.insertMarketActivity(marketActivity);
            }*/
        } catch (Exception e) {
            logger.error("新增活动失败", e);
            return TResponse.FAIL("新增活动失败");
        }
        logger.error("新增活动成功");
        return TResponse.SUCCESS("新增活动成功");
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping(value = "/deleteActivity", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteActivity(Long marketActivityId) {
        MarketActivity info = marketActivityService.selectByPrimaryKey(marketActivityId);
        if (info==null){
            return TResponse.FAIL("请求错误");
        }
        MarketActivityRecordExample childExample = new MarketActivityRecordExample();
        childExample.createCriteria().andMarketActivityIdEqualTo(marketActivityId);
        marketActivityRecordService.deleteByExample(childExample);
        marketActivityService.deleteByPrimaryKey(marketActivityId);
        return TResponse.SUCCESS();
    }


    /**
     * 返回营销活动列表数据
     *
     * @return
     */
    @RequestMapping(value = "/selectTicketDetail", method = RequestMethod.GET)
    @ResponseBody
    public TResponse selectTicketDetail(Long ticketTypeId) {
        if (ticketTypeId != null) {
            TenantTicketType tenantTicketType = tenantTicketTypeService.selectByPrimaryKey(ticketTypeId);
            return TResponse.SUCCESS_DATA(tenantTicketType);
        } else {
            return TResponse.SUCCESS_DATA(null);
        }

    }


    /**
     * 更改 充值送活动 状态
     *
     * @return
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateStatus(Long marketActivityId, String status) {
        try {
            TenantUser user = ShiroUtils.getSessionUser();

            //  if ("1".equals(status)) {
            //      Boolean executeDiscount = marketActivityService.isExecuteDiscount(user.getTenantCode());
            //      if (executeDiscount) {
            //          return TResponse.FAIL("存在未开始或进行中的活动，无法启用");
            //      }
            //  }


            MarketActivity marketActivity = marketActivityService.selectByPrimaryKey(marketActivityId);
            if ("1".equals(status)){
                if (marketActivityService.isExecuteDiscount(marketActivity.getMarketActivityId(), user.getTenantCode(), marketActivity.getAliPay(), marketActivity.getWxPay(), marketActivity.getUnPay(), marketActivity.getAbcPay(),marketActivity.getIsApp(),marketActivity.getIsWx())) {
                    return TResponse.FAIL("已存在对应支付方式的活动，请勿重复添加");
                }
            }

            marketActivity.setStatus(status);
            marketActivityService.updateByPrimaryKeySelective(marketActivity);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("更改充值送活动状态失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 判断是否存在进行中的
     *
     * @return
     */
    @RequestMapping(value = "/isExecuteDiscount", method = RequestMethod.GET)
    @ResponseBody
    public TResponse isExecuteDiscount() {
        try {
            TenantUser user = ShiroUtils.getSessionUser();
            // Boolean executeDiscount = marketActivityService.isExecuteDiscount(user.getTenantCode());
            // if (executeDiscount) {
            //     return TResponse.FAIL("存在未开始或进行中的活动，无法新增");
            // }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("查询失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

    /**
     * 判断是否存在进行中的 新用户送优惠券
     *
     * @return
     */
    @RequestMapping(value = "/isExecuteMarketActivity", method = RequestMethod.GET)
    @ResponseBody
    public TResponse isExecuteMarketActivity() {
        try {
            TenantUser user = ShiroUtils.getSessionUser();

            if (isExecute(user)) return TResponse.FAIL("已经存在此种营销活动");
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("查询失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

    private boolean isExecute(TenantUser user) {
        MarketActivityExample marketActivityExample = new MarketActivityExample();
        marketActivityExample.createCriteria().andTenantCodeEqualTo(user.getTenantCode()).andActivityTypeEqualTo("0").andStatusBetween("1", "2");
        List<MarketActivity> marketActivities = marketActivityService.selectByExample(marketActivityExample);

        if (!CollectionUtils.isEmpty(marketActivities)) {
            return true;
        }
        return false;
    }

    /**
     * 更改活动状态
     *
     * @return
     */
    @RequestMapping(value = "/updateActivityStatus", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateActivityStatus(Long marketActivityId,String status) {
        try {
            MarketActivity marketActivity = marketActivityService.selectByPrimaryKey(marketActivityId);
            marketActivity.setStatus(status);
            marketActivityService.updateByPrimaryKeySelective(marketActivity);
            return TResponse.SUCCESS_DATA(marketActivity);
        } catch (Exception e) {
            logger.error("更改活动状态失败", e);
            return TResponse.FAIL("更改活动状态失败");
        }
    }
    /*  *//**
     * 更改新用户送优惠券状态
     *
     * @return
     *//*
    @RequestMapping(value = "/updateMarketActivityStatus", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateMarketActivityStatus(Long marketActivityId) {
        try {
            MarketActivity marketActivity = marketActivityService.selectByPrimaryKey(marketActivityId);
            marketActivityService.updateMarketActivityStatus(marketActivity);
            return TResponse.SUCCESS_DATA(marketActivity);
        } catch (Exception e) {
            logger.error("更改新用户送优惠券状态失败", e);
            return TResponse.FAIL("操作失败");
        }
    }*/


    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    public String statistics(Model model) {
        return "/templates/market/activityStatistics";
    }


    /**
     * 活动统计
     *
     * @return
     */
    @RequestMapping(value = "/countMarcet", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject countMarcet(String isDayMonth) {
        return  marketActivityService.getMarKetStatistics(isDayMonth);
    }


    /**
     * 导出活动列表数据
     */
    @ResponseBody
    @RequestMapping("/exporListPageMarketActivity")
    public TResponse exporListPageMarketActivity(MarketSearch search, HttpServletResponse response) {
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        try {
            ExportUtils.setDefaultPaging(search);
            PageInfo<MarketActivity> pageInfo = marketActivityService.listPageMarketActivity(search);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("活动列表列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), MarketActivity.class).sheet("活动列表列表").doWrite(pageInfo.getList());
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

}
